package com.bcgtgjyb.huanwen.customview.mylibrary;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by bigwen on 2016/1/14.
 */
public class ManyCircle extends Component implements Component.DrawTask {
    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
    private int width;
    private int height;
    private float pi2;
    private float mRadius;
    private Paint paint;
    private int maxRadius = 16;
    private DataAnimatorValue valueAnimator;
    private boolean isInit = false;
    private float radiu = 10;

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrs 属性集
     */
    public ManyCircle(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 构造
     *
     * @param context 上下文
     */
    public ManyCircle(Context context) {
        super(context);
        init();
    }

    private void init() {
        addDrawTask(this);
        paint = new Paint();
        paint.setColor(Color.RED);
    }

    private void start() {
        if (valueAnimator == null) {
            valueAnimator = new DataAnimatorValue();
            valueAnimator.setStart(0);
            valueAnimator.setEnd(maxRadius);

            valueAnimator.setCurveType(Animator.CurveType.LINEAR);
            valueAnimator.setDuration(1000);
            valueAnimator.start();
        } else {
            valueAnimator.start();
        }
        eventHandler.postTask(new Runnable() {
            @Override
            public void run() {
                start();
                invalidate();
            }
        }, 10);

        invalidate();
    }

    // 分段函数
    private float f(float radius) {
        if (radius <= maxRadius / 2d) {
            return radius;
        } else if (radius < maxRadius) {
            return maxRadius - radius;
        } else if (radius < maxRadius * 3d / 2d) {
            return radius - maxRadius;
        } else {
            return (float) (2d * maxRadius - radius);
        }
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (!isInit) {
            isInit = true;
            start();
            width = getWidth() / 2;
            height = getHeight() / 2;
            pi2 = 2 * (float) Math.PI;
            mRadius = width - maxRadius;
        }

        canvas.drawCircle((float) (width + mRadius * Math.sin(0)),
                (float) (height + mRadius * Math.cos(0)), f(radiu + 0), paint);
        canvas.drawCircle((float) (width + mRadius * Math.sin(pi2 / 8)),
                (float) (height + mRadius * Math.cos(pi2 / 8)), f(radiu + 2), paint);
        canvas.drawCircle((float) (width + mRadius * Math.sin(pi2 / 8 * 2)),
                (float) (height + mRadius * Math.cos(pi2 / 8 * 2)), f(radiu + 4), paint);
        canvas.drawCircle((float) (width + mRadius * Math.sin(pi2 / 8 * 3)),
                (float) (height + mRadius * Math.cos(pi2 / 8 * 3)), f(radiu + 6), paint);

        canvas.drawCircle((float) (width + mRadius * Math.sin(pi2 / 8 * 4)),
                (float) (height + mRadius * Math.cos(pi2 / 8 * 4)), f(radiu + 8), paint);
        canvas.drawCircle((float) (width + mRadius * Math.sin(pi2 / 8 * 5)),
                (float) (height + mRadius * Math.cos(pi2 / 8 * 5)), f(radiu + 10), paint);
        canvas.drawCircle((float) (width + mRadius * Math.sin(pi2 / 8 * 6)),
                (float) (height + mRadius * Math.cos(pi2 / 8 * 6)), f(radiu + 12), paint);
        canvas.drawCircle((float) (width + mRadius * Math.sin(pi2 / 8 * 7)),
                (float) (height + mRadius * Math.cos(pi2 / 8 * 7)), f(radiu + 14), paint);

        if (valueAnimator.isRunning()) {
            radiu = (float) valueAnimator.getAnimatedValue();
        }
    }
}
